package com.missfresh.cooperation2meituan.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.missfresh.cooperation2meituan.service.OrderExtendService;
import com.missfresh.cooperation2meituan.service.OrderService;
import com.missfresh.cooperation2meituan.util.CommonUtil;
import com.missfresh.cooperation2meituan.util.MD5Util;
import com.missfresh.cooperation2meituan.vo.Order;
import com.missfresh.cooperation2meituan.vo.PageView;
import com.missfresh.cooperation2meituan.vo.QueryResult;

@Controller
@RequestMapping("employee")
public class EmployeeLogon {
	
	private static final Logger logger = LoggerFactory.getLogger(EmployeeLogon.class);
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderExtendService orderExtendService;
	
	@RequestMapping("/storequery.do")
	public <T> ModelAndView  storequery(@RequestParam Map<String,Object> params,Model model) {
		CommonUtil.iteratorMap(params);
		ModelAndView mv = new ModelAndView("book/storelist");
		
		PageView<Order> pageView = new PageView<Order>(12, 1);
		
		QueryResult qr = new QueryResult<T>();
		List<Map<String,Object>> datas =  orderExtendService.queryMeituanInventoryAppstore(params);
		qr.setResultlist(datas);
		qr.setTotalrecord(100);//mysql返回long
		pageView.setQueryResult(qr);
		model.addAttribute("pageView", pageView);
		
		return mv;
	}
	@RequestMapping("/orderquery.do")
	public <T> ModelAndView  orderquery(@RequestParam Map<String,Object> params,Model model) {
		CommonUtil.iteratorMap(params);
		ModelAndView mv = new ModelAndView("book/orderlist");
		
		PageView<Order> pageView = new PageView<Order>(12, 1);
		
		QueryResult qr = new QueryResult<T>();
		List<Map<String,Object>> datas = orderService.queryMeituanOrders(CommonUtil.preparePage(params));
		qr.setResultlist(datas);
		qr.setTotalrecord(orderService.queryMeituanOrdersCount(params));//mysql返回long
		pageView.setQueryResult(qr);
		model.addAttribute("pageView", pageView);
		
		return mv;
	}
	
	@RequestMapping("/logon.do")
	public String logon(@RequestParam Map<String,Object> params,HttpServletRequest request) {
		CommonUtil.iteratorMap(params);
		String username=(String)params.get("username");
		String password=(String)params.get("password");
		String checkcode=(String)params.get("checkcode");
		if(username!=null){
			if(checkcode!=null&&checkcode.equals(request.getSession().getAttribute("code"))){
				if("admin".equals(username)&&MD5Util.MD5("mryxMRYX!QA").equals(MD5Util.MD5(password))){
					//进入欢迎页
					request.getSession().setAttribute("employee", "admin");
					return "controlcenter/default";
				}else{
					if(username!=null||password!=null){
						request.setAttribute("message", "用户名或密码有误");
					}
				}
			}else{
				request.setAttribute("message", "验证码错误");	
			}
		}
		return "department/logon";
	}
	
	@RequestMapping("/logout.do")
	public String logout(@RequestParam Map<String,Object> params,HttpServletRequest request){
		request.getSession().removeAttribute("employee");
		return "department/logon";
	}
	
	@RequestMapping("/route.do")
	public String route(@RequestParam Map<String,String> params) {
		String m = params.get("m");
		if("top".equals(m)){
			return "controlcenter/top";
		}else if("menu".equals(m)){
			return "controlcenter/menu";
		}else if("right".equals(m)){
			return "controlcenter/right";
		}else if("end".equals(m)){
			return "controlcenter/end";
		}else if("bookquery".equals(m)){
			return "book/queryorder";
		}else if("storequery".equals(m)){
			return "book/querystore";
		}
		return "";
	}
	
}

